﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using ECI.Entities;
using ECI.DynamicData.ElementBuilder;
using ECI.Controls;


namespace ECI.DynamicData
{
    public class DynamicGridView
    {
        /// <summary>
        /// 创建数据结构
        /// </summary>
        public static void CreateStruct(GridView dataGrid, EciTable table)
        {
            BoundField bf = new BoundField();
            string numberColumn = "";
            int a = 1;
            foreach (EciField field in table.ListShow)
            {
                if (field.Udf7 != "")
                {
                    if (!ECI.Framework.Authority.HasRight(field.Udf7))
                    {
                        continue;
                    }
                }
                if (a == table.ListShow.Count)
                {
                    bf = new BoundField();
                    bf.HeaderText = "KEY_0";
                    bf.DataField = "KEY_0";
                    bf.HeaderStyle.Width = 0;
                    bf.ItemStyle.Width = 0;
                    dataGrid.Columns.Add(bf);
                }
                a++;
                bf = new BoundField();
                bf.HeaderText = field.MainAlias;
                bf.DataField = field.Name;
                bf.ReadOnly = true;
                if (field.Width.HasValue && field.Width.Value != 0)
                {
                    if (field.Width.Value > 0)
                    {
                        bf.HeaderStyle.Width = new Unit(field.Width.Value, UnitType.Pixel);
                        bf.ItemStyle.Width = new Unit(field.Width.Value, UnitType.Pixel);
                    }
                    else
                    {
                        bf.HeaderStyle.Width = 0;
                        bf.ItemStyle.Width = 0;
                    }
                }
                else
                {
                    bf.HeaderStyle.Width = new Unit(200, UnitType.Pixel);
                    bf.ItemStyle.Width = new Unit(200, UnitType.Pixel);
                }

                //设置字段 水平位置 HEAD
                if (field.GridHeadAlign.ToUpper() == "LEFT")
                {
                    bf.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
                }
                //设置字段 水平位置 
                if (field.GridItemAlign.ToUpper() == "CENTER")
                {
                    bf.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                }

                bf.SortExpression = field.Name;
                dataGrid.Columns.Add(bf);


                if (field.PhysicalType == "NUMBER")
                {
                    numberColumn += "," + field.MainAlias;
                }
            }

            if (numberColumn.Length > 0)
            {
                numberColumn = numberColumn.Substring(1);
                System.Web.UI.HtmlControls.HtmlForm htmlForm = ((System.Web.UI.HtmlControls.HtmlForm)(ECI.CommonLibrary.PageHelper.CurrentPage.FindControl("form1")));
                htmlForm.Attributes["NumberColumn"] = numberColumn;
            }
        }

        /// <summary>
        /// 获取ECIGridView数据源
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public static ECIGridViewData GetGridViewData(EciTable table)
        {
            ECIGridViewData data = new ECIGridViewData();
            data.GridTemplate = table.GridTemplate;
            if (string.IsNullOrEmpty(data.GridTemplate)) data.GridTemplate = "N";
            if (data.GridTemplate == "N") return data;
            data.ListElement = new List<ECIGridViewDataElement>();
            ECIGridViewDataElement element = null;
            foreach (EciField field in table.ListShow)
            {
                element = new ECIGridViewDataElement();
                element.DictionaryType = field.DictionaryType;
                element.Name = field.Name;
                element.MainAlias = field.MainAlias;
                element.DictionaryTypeConfig = field.DictionaryTypeConfig;
                element.Width = field.Width;
                element.ControlWidth = field.ControlWidth;
                element.GridHeadAlign = field.GridHeadAlign;
                element.GridItemAlign = field.GridItemAlign;
                element.GridTemplate = field.GridTemplate;

                data.ListElement.Add(element);
            }

            return data;
        }
    }
}


